﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;
using System.Reflection;
using System.Globalization;
using System.IO;
using System.Data;
using System.Xml.Schema;
using System.Text.RegularExpressions;

namespace Eugenhome.Atrium
{

    /// <summary>
    /// 
    /// </summary>
    public static class ExtClass
    {

        #region FROM STRING CONVERT METHODS


        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static double ToDouble(this string str)
        {
            return str.ToDouble(0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static double ToDouble(this string str, double defaultValue)
        {
            double result = defaultValue;

            if (!double.TryParse(str, out result))
            {
                result = defaultValue;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string str)
        {
            return str.ToDateTime(DateTime.MinValue);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string str, DateTime defaultValue)
        {
            DateTime result = defaultValue;

            if (!DateTime.TryParse(str, out result))
            {
                result = defaultValue;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int ToInt(this string str)
        {
            return str.ToInt(0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int ToInt(this string str, int defaultValue)
        {
            if (string.IsNullOrEmpty(str))
                return defaultValue;

            int result = defaultValue;

            if (str != null && !str.Trim().Equals(string.Empty))
            {
                str = str.Split(new char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries)[0];

                if (!int.TryParse(str, out result))
                {
                    result = defaultValue;
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static long ToLong(this string str)
        {
            return str.ToLong(0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static long ToLong(this string str, long defaultValue)
        {
            long result = defaultValue;

            if (!long.TryParse(str, out result))
            {
                result = defaultValue;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static float ToFloat(this string str)
        {
            return str.ToFloat(0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static float ToFloat(this string str, float defaultValue)
        {
            float result = defaultValue;

            if (!float.TryParse(str, out result))
            {
                result = defaultValue;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool ToBool(this string str, bool defaultValue)
        {
            bool result = defaultValue;

            if (!Boolean.TryParse(str, out result))
            {
                result = defaultValue;
            }

            return result;
        }

        /// <summary>
        /// String mit Default-Separatoren ';', ',', '|' trennen
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<string> ToList(this string str)
        {
            return str.ToList(new string[] { ";", ",", "|" }, false, new List<string>());
        }

        /// <summary>
        /// String mit Default-Separatoren ';', ',', '|' trennen
        /// </summary>
        /// <param name="str"></param>
        /// <param name="trim">Einzelne getrennte Items müssen von Anfangs- und End-Leerzeichen befreit werden</param>
        /// <returns></returns>
        public static List<string> ToList(this string str, bool trim)
        {
            return str.ToList(new string[] { ";", ",", "|" }, trim, new List<string>());
        }

        /// <summary>
        /// String mit Default-Separatoren ';', ',', '|' trennen
        /// </summary>
        /// <param name="str"></param>
        /// <param name="trim">Einzelne getrennte Items müssen von Anfangs- und End-Leerzeichen befreit werden</param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static List<string> ToList(this string str, bool trim, List<string> defaultValue)
        {
            return str.ToList(new string[] { ";", ",", "|" }, trim, defaultValue);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="separators"></param>
        /// <param name="trim"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static List<string> ToList(this string str, string[] separators, bool trim, List<string> defaultValue)
        {
            List<string> result = defaultValue;

            try
            {
                if (trim)
                {
                    result = str.Split(separators, StringSplitOptions.RemoveEmptyEntries).Select(item => item.Trim()).ToList<string>();
                }
                else
                {
                    result = str.Split(separators, StringSplitOptions.RemoveEmptyEntries).ToList<string>();
                }
            }
            catch
            {
                result = defaultValue;
            }

            return result;
        }


        /// <summary>
        /// returns true if the string can be parsed
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumeric(this string str)
        {
            long result = 0;
            return long.TryParse(str, out result);
        }

        #endregion FROM STRING CONVERT METHODS

        #region ENUMERABLE

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string ToString<T>(this IEnumerable<T> source, string separator)
        {
            if (source == null)
                throw new ArgumentException("Parameter source can not be null.");

            if (string.IsNullOrEmpty(separator))
                throw new ArgumentException("Parameter separator can not be null or empty.");

            string[] array = source.Where(n => n != null).Select(n => n.ToString()).ToArray();

            return string.Join(separator, array);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string ToString(this IEnumerable source, string separator)
        {
            if (source == null)
                throw new ArgumentException("Parameter source can not be null.");

            if (string.IsNullOrEmpty(separator))
                throw new ArgumentException("Parameter separator can not be null or empty.");

            string[] array = source.Cast<object>().Where(n => n != null).Select(n => n.ToString()).ToArray();

            return string.Join(separator, array);
        }

        #endregion enumerable

        #region ENUMERATION

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="enums"></param>
        /// <returns></returns>
        public static bool Any(this Enum source, params Enum[] enums)
        {
            return enums.Any(item => item.Equals(source));
        }

        #endregion enumeration

        #region FROM OBJECT CONVERT METHODS

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool ToBool(this object obj, bool defaultValue)
        {
            bool result = defaultValue;

            try
            {
                result = Convert.ToBoolean(obj);
            }
            catch
            {
                result = defaultValue;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static double ToDouble(this object obj)
        {
            return (obj != null) ? obj.ToString().ToDouble() : "".ToDouble();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this object obj)
        {
            return (obj != null) ? obj.ToString().ToDateTime() : "".ToDateTime();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int ToInt(this object obj)
        {
            return (obj != null) ? obj.ToString().ToInt() : "".ToInt();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static long ToLong(this object obj)
        {
            return (obj != null) ? obj.ToString().ToLong() : "".ToLong();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static float ToFloat(this object obj)
        {
            return (obj != null) ? obj.ToString().ToFloat() : "".ToFloat();
        }

		/// <summary>
		/// Converts to float.
		/// </summary>
		/// <param name="obj">The obj.</param>
		/// <param name="defaultValue">The default value.</param>
		/// <returns></returns>
        public static float ToFloat(this object obj, float defaultValue)
        {
            return (obj != null) ? obj.ToString().ToFloat(defaultValue) : "".ToString().ToFloat();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="byteDataSize"></param>
        /// <returns></returns>
        public static string ToFormatDataSize(this object byteDataSize)
        {
            long bytes = byteDataSize.ToLong();

            if (bytes >= 1073741824)
            {
                Decimal size = Decimal.Divide(bytes, 1073741824);
                return String.Format("{0:##.##} GB", size);
            }
            else if (bytes >= 1048576)
            {
                Decimal size = Decimal.Divide(bytes, 1048576);
                return String.Format("{0:##.##} MB", size);
            }
            else if (bytes >= 1024)
            {
                Decimal size = Decimal.Divide(bytes, 1024);
                return String.Format("{0:##.##} KB", size);
            }
            else if (bytes > 0 & bytes < 1024)
            {
                Decimal size = bytes;
                return String.Format("{0:##.##} Bytes", size);
            }
            else
            {
                return "0 Bytes";
            }


            //string ssize = "";
            //if (bytedatasize / 1024 > 1024)
            //{
            //    double size = (bytedatasize / 1024) / 1024;
            //    ssize = size.ToString("#######0.### MB");
            //}
            //else if (bytedatasize > 1024)
            //{
            //    double size = bytedatasize / 1024;
            //    ssize = size.ToString("#######0.### KB");
            //}
            //else
            //{
            //    ssize = bytedatasize.ToString("#######0.### Byte");
            //}
            //return ssize;
        }

        #endregion FROM OBJECT CONVERT METHODS

        #region TIMESTAMP CONVERT METHODS

        /// <summary>
        /// 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static int ToTimestamp(this DateTime date)
        {
            DateTime date1 = new DateTime(1970, 1, 1);  //Refernzdatum (festgelegt)
            TimeSpan ts = new TimeSpan(date.Ticks - date1.Ticks);  // das Delta ermitteln
            return (Convert.ToInt32(ts.TotalSeconds));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime ToTimestam(this int timestamp)
        {
            DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
            dateTime = dateTime.AddSeconds(timestamp);
            return dateTime;
        }

        #endregion TIMESTAMP CONVERT METHODS

        #region XMLDOCUMENT

        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributename"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static DateTime GetTryValue(this XmlNode node, string attributename, DateTime defaultValue)
        {
            DateTime value = defaultValue;
            if (node != null && node.Attributes[attributename] != null)
            {
                string valueString = node.Attributes[attributename].Value;
                if (!DateTime.TryParse(valueString, out value))
                {
                    value = defaultValue;
                }
            }

            return value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributename"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int GetTryValue(this XmlNode node, string attributename, int defaultValue)
        {
            int value = defaultValue;
            if (node != null && node.Attributes[attributename] != null)
            {
                string valueString = node.Attributes[attributename].Value;
                if (!int.TryParse(valueString, out value))
                {
                    value = defaultValue;
                }
            }

            return value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributename"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool GetTryValue(this XmlNode node, string attributename, bool defaultValue)
        {
            bool value = defaultValue;
            if (node != null && node.Attributes[attributename] != null)
            {
                string valueString = node.Attributes[attributename].Value;
                if (!bool.TryParse(valueString, out value))
                {
                    value = defaultValue;
                }
            }
            return value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributename"></param>
        /// <returns></returns>
        public static string GetTryValue(this XmlNode node, string attributename)
        {
            return GetTryValue(node, attributename, "");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributename"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetTryValue(this XmlNode node, string attributename, string defaultValue)
        {
            string value = defaultValue;
            if (node != null && node.Attributes[attributename] != null)
            {
                value = node.Attributes[attributename].Value;
            }

            return value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributename"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetTryValue(this XmlNode node, string attributename, object value)
        {
            return SetTryValue(node, attributename, value, false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attributename"></param>
        /// <param name="value"></param>
        /// <param name="createIfNotExists"></param>
        /// <returns></returns>
        public static bool SetTryValue(this XmlNode node, string attributename, object value, bool createIfNotExists)
        {
            bool result = false;

            if (node != null)
            {
                XmlAttribute attr = node.Attributes[attributename];
                if (attr == null && createIfNotExists)
                {
                    attr = node.OwnerDocument.CreateAttribute(attributename);
                    node.Attributes.Append(attr);
                }

                if (attr != null)
                {
                    if (value is bool)
                    {
                        bool val = false;
                        if (bool.TryParse(value.ToString(), out val))
                        {
                            attr.Value = val.ToString().ToLower();
                            result = true;
                        }
                    }
                    else if (value != null)
                    {
                        attr.Value = value.ToString();
                        result = true;
                    }
                    else if (value == null)
                    {
                        node.Attributes.Remove(attr);
                        result = true;
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="nodename"></param>
        /// <param name="value"></param>
        /// <param name="createIfNotExists"></param>
        /// <returns></returns>
        public static bool SetTryText(this XmlNode parentNode, string nodename, object value, bool createIfNotExists)
        {
            bool result = false;

            if (parentNode != null)
            {
                XmlNodeList nodes = parentNode.SelectNodes(nodename);

                if (nodes.Count.Equals(0) && createIfNotExists)
                {
                    XmlNode node = parentNode.OwnerDocument.CreateElement(nodename);
                    parentNode.AppendChild(node);
                }

                foreach (XmlNode node in parentNode.SelectNodes(nodename))
                {
                    if (value is bool)
                    {
                        bool val = false;
                        if (bool.TryParse(value.ToString(), out val))
                        {
                            node.InnerText = val.ToString().ToLower();
                            result = true;
                        }
                    }
                    else
                    {
                        node.InnerText = value.ToString();
                        result = true;
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="nodename"></param>
        /// <returns></returns>
        public static string GetTryText(this XmlNode parentNode, string nodename)
        {
            return GetTryText(parentNode, nodename, "");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="nodename"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int GetTryText(this XmlNode parentNode, string nodename, int defaultValue)
        {
            return GetTryText(parentNode, nodename, defaultValue.ToString()).ToInt();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="nodename"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static DateTime GetTryText(this XmlNode parentNode, string nodename, DateTime defaultValue)
        {
            return GetTryText(parentNode, nodename, defaultValue.ToString()).ToDateTime();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="nodename"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static long GetTryText(this XmlNode parentNode, string nodename, long defaultValue)
        {
            return GetTryText(parentNode, nodename, defaultValue.ToString()).ToLong();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="nodename"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetTryText(this XmlNode parentNode, string nodename, string defaultValue)
        {
            string value = defaultValue;
            if (parentNode != null && parentNode.SelectSingleNode(nodename) != null)
            {
                value = parentNode.SelectSingleNode(nodename).InnerXml;
            }

            return value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="nodename"></param>
        /// <returns></returns>
        public static XmlNode AddNodeTry(this XmlNode parentNode, string nodename)
        {
            XmlDocument ownerDocument = parentNode != null ? parentNode.OwnerDocument : null;

            XmlNode node = null;

            if (ownerDocument != null)
            {
                try
                {
                    node = ownerDocument.CreateElement(nodename);

                    parentNode.AppendChild(node);
                }
                catch { }
            }
            return node;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="nodenames"></param>
        /// <returns></returns>
        public static XmlNode[] AddNodesTry(this XmlNode parentNode, params string[] nodenames)
        {
            XmlDocument ownerDocument = parentNode != null ? parentNode.OwnerDocument : null;

            XmlNode[] nodes = null;

            if (ownerDocument != null)
            {
                try
                {
                    nodes = nodenames.Select(item => ownerDocument.CreateElement(item)).ToArray();

                    foreach (XmlNode node in nodes)
                    {
                        parentNode.AppendChild(node);
                    }
                }
                catch { }
            }
            return nodes;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ownerDocument"></param>
        /// <param name="nodename"></param>
        /// <returns></returns>
        public static XmlNode AddNodeTry(this XmlDocument ownerDocument, string nodename)
        {
            XmlNode node = null;

            if (ownerDocument != null)
            {
                try
                {
                    node = ownerDocument.CreateElement(nodename);

                    ownerDocument.AppendChild(node);
                }
                catch { }
            }

            return node;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ownerDocument"></param>
        /// <param name="xsdFiles"></param>
        /// <returns></returns>
        public static bool SetSchemaTry(this XmlDocument ownerDocument, params Uri[] xsdFiles)
        {
            //Prüfen ob alle XSD-Dateien existieren
            bool result = (ownerDocument != null) && !xsdFiles.Any(item => ((item == null) || (item != null && !File.Exists(item.LocalPath))));

            //Schema setzen
            return result ? SetSchemaTry(ownerDocument, xsdFiles.Select(item => File.ReadAllText(item.LocalPath)).ToArray()) : result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ownerDocument"></param>
        /// <param name="xsdContents"></param>
        /// <returns></returns>
        public static bool SetSchemaTry(this XmlDocument ownerDocument, params string[] xsdContents)
        {
            bool result = false;

            if (ownerDocument != null)
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;

                try
                {
                    foreach (string xsdContent in xsdContents)
                    {
                        settings.Schemas.Add(null, XmlReader.Create(new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(xsdContent))));
                    }

                    //XmlReader myReader = XmlReader.Create(xmlfile, settings);
                    MemoryStream ms = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(ownerDocument.OuterXml));
                    ms.Position = 0;
                    XmlReader myReader = XmlReader.Create(ms, settings);

                    ownerDocument = new XmlDocument();
                    ownerDocument.Load(myReader);

                    myReader.Close();

                    return true;
                }
                catch (Exception ex)
                {
                }
            }

            return result;
        }

        #region Compare XML Docs
        /// <summary>
        /// 
        /// </summary>
        /// <param name="doc1"></param>
        /// <param name="doc2"></param>
        /// <returns>
        /// False - elements are not equal
        /// True - elements are equal
        /// </returns>
        public static bool EqualNodewise(this XmlDocument doc1, XmlDocument doc2)
        {
            bool result = false;
            // Compare the Root nodes.

            result = CompareNode(doc1.ChildNodes[0], doc2.ChildNodes[0], true);
            
            return result;
        }

        /// <summary>
        /// Compare the nodes recursive
        /// 1. compare attributes 
        /// 2. compare children nodes
        /// </summary>
        /// <param name="node1"></param>
        /// <param name="node2"></param>
        /// <param name="compareAttributes"></param>
        /// <returns>
        /// False - elements are not equal
        /// True - elements are equal
        /// </returns>
        private static bool CompareNode(XmlNode node1, XmlNode node2, bool compareAttributes)
        {
            bool result = false;
            if (
                node1.Attributes != null
                && node2.Attributes != null
                && node1.Attributes.Count != node2.Attributes.Count
                )
                return false;

            if (compareAttributes && (node1.Attributes != null))
            {
                foreach (XmlAttribute att in node1.Attributes)
                {
                    result = CompareAttribute(att, node2);
                    if (!result)
                        return false;
                }
            }
            if (node1.ChildNodes.Count > 0)
            {
                foreach (XmlNode childNode in node1.ChildNodes)
                {
                    if (node2.ChildNodes.Count > 0)
                    {
                        foreach (XmlNode childNode2 in node2.ChildNodes)
                        {
                            if (childNode.Attributes != null
                                && childNode2.Attributes != null)
                            {
                                if (childNode.Attributes["Source"].Value == childNode2.Attributes["Source"].Value)
                                {
                                    result = CompareNode(childNode, childNode2, true);

                                    if (!result)
                                        break;
                                }
                            }
                            else
                            {
                                result = CompareNode(childNode, childNode2, false);
                            }
                        }
                    }
                    if (!result)
                        break;
                }
            }
            else
            {
                result = (((XmlNode)node1).Value == ((XmlNode)node2).Value);
            }
            return result;
        }

        /// <summary>
        /// compare attributes 
        /// </summary>
        /// <param name="comparendAtt"></param>
        /// <param name="node"></param>
        /// <returns>
        /// False - attributes are not equal
        /// True - attributes are equal
        /// </returns>
        private static bool CompareAttribute(XmlAttribute comparendAtt, XmlNode node)
        {
            bool result = false;

            foreach (XmlAttribute att in node.Attributes)
            {
                if (comparendAtt.Name == att.Name)
                {
                    if (comparendAtt.Value == att.Value)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }

            return result;
        }
        #endregion


        #endregion XMLDOCUMENT

        #region EXCEPTION

        /// <summary>
        /// Alle InnerException in string
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="depth">Tiefe der Rekursion</param>
        /// <returns></returns>
        public static string GetFullExceptionText(this Exception ex, int depth)
        {
            if (ex == null)
                return "";

            string result = ex.Message;
            if (ex.InnerException != null && depth > 0)
            {
                result += "\n\n" + ex.InnerException.Message;
                result += GetFullExceptionText(ex.InnerException, --depth);
            }
            return result;
        }

        #endregion EXCEPTION



        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetNumber(this string str)
        {
            return GetNumber(str, "");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string GetNumber(this string str, string defaultValue)
        {
            var match = Regex.Match(str, @"\d+");

            if (match.Success)
            {
                return match.Value;
            }
            else
            {
                return defaultValue;
            }
        }

        #region DataSet

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="xmlFile"></param>
        /// <param name="schemaFile">Wenn ohne Schema dann null</param>
        /// <returns></returns>
        public static bool ReadXmlTry(this DataSet ds, Uri xmlFile, Uri schemaFile)
        {
            bool result = true;

            try
            {
                if (schemaFile != null)
                {
                    ds.ReadXmlSchema(schemaFile.LocalPath);
                }

                ds.ReadXml(xmlFile.LocalPath);
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="xmlFile"></param>
        /// <returns></returns>
        public static bool ReadXmlTry(this DataSet ds, Uri xmlFile)
        {
            return ds.ReadXmlTry(xmlFile, (Uri)null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="xmlFile"></param>
        /// <param name="schemaContent">Wenn ohne Schema dann leer lassen</param>
        /// <returns></returns>
        public static bool ReadXmlTry(this DataSet ds, Uri xmlFile, string schemaContent)
        {
            bool result = true;

            try
            {
                if (!string.IsNullOrEmpty(schemaContent))
                {
                    MemoryStream schemaStream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(schemaContent));

                    ds.ReadXmlSchema(schemaStream);
                }

                ds.ReadXml(xmlFile.LocalPath);
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="xmlContent"></param>
        /// <param name="schemaContent">Wenn ohne Schema dann leer lassen</param>
        /// <returns></returns>
        public static bool ReadXmlTry(this DataSet ds, string xmlContent, string schemaContent)
        {
            bool result = true;

            try
            {
                if (!string.IsNullOrEmpty(schemaContent))
                {
                    MemoryStream schemaStream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(schemaContent));
                    ds.ReadXmlSchema(schemaStream);
                }

                MemoryStream xmldataStream = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(xmlContent));
                ds.ReadXml(xmldataStream);
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="schemaFileUri"></param>
        /// <returns></returns>
        public static bool ReadXmlSchemaTry(this DataSet ds, Uri schemaFileUri)
        {
            bool result = true;

            if (File.Exists(schemaFileUri.LocalPath))
            {
                try
                {
                    ds.ReadXmlSchema(schemaFileUri.LocalPath);
                }
                catch (Exception ex)
                {
                    result = false;
                }
            }
            else
            {
                result = false;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="schemaContent">Wenn ohne Schema dann leer lassen (bestehende Schema wird NICHT entfernt)</param>
        /// <returns></returns>
        public static bool ReadXmlSchemaTry(this DataSet ds, string schemaContent)
        {
            bool result = true;

            if (!string.IsNullOrEmpty(schemaContent))
            {
                try
                {
                    MemoryStream schemaStream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(schemaContent));

                    ds.ReadXmlSchema(schemaStream);
                }
                catch (Exception ex)
                {
                    result = false;
                }
            }

            return result;
        }

        #endregion dataset


    }
}
